# -*- coding: utf-8 -*-

# @Project : UI_DDTFrameWork
# @Author  : Mr.Deng
# @Time    : 2021/9/21 21:28

"""
unittest 框架中断言方法二次封装，判断相等、不等、包含、不包含、为真、为假、误差值、为None、不为None
"""

from page import BasePage
from common.tools.decorator import error_collection

from unittest import TestCase as Ts


class Assertion(BasePage):

    @error_collection
    def assert_in(self, expectation: str, faction: str or list):
        """
        判断期望值在实际值中
        :param expectation: 期望值
        :param faction: 实际值
        :return:
        """
        try:
            Ts().assertIn(expectation, faction)
        except AssertionError:
            self.driver.screen_shot()
            raise AssertionError(f"【断言】- 期望值：{expectation} 不在实际值：{faction} 中, 测试失败！！！")
        else:
            self.driver.Log.info(f"【断言】- 期望值：{expectation} 在实际值：{faction} 中, 测试通过！")

    @error_collection
    def assert_not_in(self, expectation: str, faction: str or list):
        """
        判断期望值不在实际值中
        :param expectation: 期望值
        :param faction: 实际值
        :return:
        """
        try:
            Ts().assertNotIn(expectation, faction)
        except AssertionError:
            self.driver.screen_shot()
            raise AssertionError(f"【断言】- 期望值：{expectation} 在实际值：{faction} 中, 测试失败！！！")
        else:
            self.driver.Log.info(f"【断言】- 期望值：{expectation} 不在实际值：{faction} 中, 测试通过！")

    @error_collection
    def assert_equal(self, expectation: str, faction: str):
        """
        判断期望值等于实际值中
        :param expectation: 期望值
        :param faction: 实际值
        :return:
        """
        try:
            Ts().assertEqual(expectation, faction)
        except AssertionError:
            self.driver.screen_shot()
            raise AssertionError(f"【断言】- 期望值：{expectation} 不等于实际值：{faction}, 测试失败！！！")
        else:
            self.driver.Log.info(f"【断言】- 期望值：{expectation} 等于实际值：{faction}, 测试通过！")

    @error_collection
    def assert_not_equal(self, expectation: str, faction: str):
        """
        判断期望值不等于实际值中
        :param expectation: 期望值
        :param faction: 实际值
        :return:
        """
        try:
            Ts().assertNotEqual(expectation, faction)
        except AssertionError:
            self.driver.screen_shot()
            raise AssertionError(f"【断言】- 期望值：{expectation} 等于实际值：{faction}, 测试失败！！！")
        else:
            self.driver.Log.info(f"【断言】- 期望值：{expectation} 不等于实际值：{faction}, 测试通过！")

    @error_collection
    def assert_true(self, faction: bool):
        """
        判断True
        :param faction: 实际值
        :return:
        """
        try:
            Ts().assertTrue(faction)
        except AssertionError:
            self.driver.screen_shot()
            raise AssertionError(f"【断言】- 实际值：{faction} 不为 True, 测试失败！！！")
        else:
            self.driver.Log.info(f"【断言】- 实际值：{faction} 等于 True, 测试通过！")

    @error_collection
    def assert_false(self, faction: bool):
        """
        判断False
        :param faction: 实际值
        :return:
        """
        try:
            Ts().assertFalse(faction)
        except AssertionError:
            self.driver.screen_shot()
            raise AssertionError(f"【断言】- 实际值：{faction} 不为 False, 测试失败！！！")
        else:
            self.driver.Log.info(f"【断言】- 实际值：{faction} 等于 False, 测试通过！")

    @error_collection
    def assert_almost_equal(self, expectation: float, faction: float, places: int = None, delta: float = 0.01):
        """
        无限接近于，例如：1.234 无限接近 1.235，一般用于判断小数问题判断
        ** places 和 delta 值不能同时存在，否则源码报错
        :param delta: 误差值范围，小于或等级改值通过
        :param places: 两位小数差值取值位数，默认 7
        :param expectation: 期望值
        :param faction: 实际值
        :return:
        """
        try:
            Ts().assertAlmostEqual(expectation, faction, places=places, delta=delta)
        except AssertionError:
            self.driver.screen_shot()
            raise AssertionError(f"【断言】- 期望值：{expectation} 与实际值：{faction} 误差大于 {delta}, 测试失败！！！")
        else:
            self.driver.Log.info(f"【断言】- 期望值：{expectation} 与实际值：{faction} 误差小于或等于 {delta}, 测试通过！")

    @error_collection
    def assert_None(self, faction: object):
        """
        判断为None
        :param faction: 实际值
        :return:
        """
        try:
            Ts().assertIsNone(faction)
        except AssertionError:
            self.driver.screen_shot()
            raise AssertionError(f"【断言】- 实际值：{faction} 不为 None, 测试失败！！！")
        else:
            self.driver.Log.info(f"【断言】- 实际值：{faction} 为 None, 测试通过！")

    @error_collection
    def assert_not_None(self, faction: object):
        """
        判断不为None
        :param faction: 实际值
        :return:
        """
        try:
            Ts().assertIsNotNone(faction)
        except AssertionError:
            self.driver.screen_shot()
            raise AssertionError(f"【断言】- 实际值：{faction} 为 None, 测试失败！！！")
        else:
            self.driver.Log.info(f"【断言】- 实际值：{faction} 不为 None, 测试通过！")

#
# if __name__ == '__main__':
#     Assertion().assert_almost_equal(expectation=1.1235, faction=1.1246)
